package com.codbking.calendar.exaple;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.codbking.calendar.CaledarAdapter;
import com.codbking.calendar.CalendarBean;
import com.codbking.calendar.CalendarDateView;
import com.codbking.calendar.CalendarUtil;
import com.codbking.calendar.CalendarView;
import com.codbking.calendar.exaple.dialog.InCommonDialog;
import com.codbking.calendar.exaple.greendao.gen.PayInfoBeanDao;
import com.codbking.calendar.exaple.greendao.gen.TodayWorkDao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class XiaomiActivity extends AppCompatActivity {

    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.calendarDateView)
    CalendarDateView mCalendarDateView;
    @BindView(R.id.list)
    ListView mList;
    @BindView(R.id.tvModuleAdd)
    TextView tvModuleAdd;

    int[] data;
    String dataTime;

    PayInfoBeanDao payInfoBeanDao = GreenDaoUtils.getSingleTon().getmDaoSession().getPayInfoBeanDao();
    TodayWorkDao todayWorkDao = GreenDaoUtils.getSingleTon().getmDaoSession().getTodayWorkDao();
    List<TodayWork> list = new ArrayList<>();
    List<TodayWork> listSecond = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiaomi);
        ButterKnife.bind(this);
        initView();
    }



    private void initView() {

        data = CalendarUtil.getYMD(new Date());
        dataTime = data[0] + "-" + data[1] + "-" + data[2];
        mTitle.setText(dataTime);

        mCalendarDateView.setAdapter(new CaledarAdapter() {
            @Override
            public View getView(View convertView, ViewGroup parentView, CalendarBean bean) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(parentView.getContext()).inflate(R.layout.item_xiaomi, null);
                }
                TextView chinaText = (TextView) convertView.findViewById(R.id.chinaText);
                TextView text = (TextView) convertView.findViewById(R.id.text);
                text.setText("" + bean.day);
                if (bean.mothFlag != 0) {
                    text.setTextColor(0xff9299a1);
                } else {
                    text.setTextColor(0xff444444);
                }
                chinaText.setText(bean.chinaDay);
                return convertView;
            }
        });

        mCalendarDateView.setOnItemClickListener(new CalendarView.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int postion, CalendarBean bean) {
                Log.e("log", "点击日期-->" + bean.year + "-" + bean.moth + "-" + bean.day);
                dataTime = bean.year + "-" + bean.moth + "-" + bean.day;
                mTitle.setText(dataTime);
                makeInfo();
            }
        });

        tvModuleAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InCommonDialog dialog = new InCommonDialog(XiaomiActivity.this);
                dialog.show();
                dialog.setOnBHClickListener(new OnBHClickListener() {
                    @Override
                    public void onConfirmClick(String info) {
                        TodayWork todayWork = new TodayWork();
                        todayWork.setWhen(dataTime);
                        todayWork.setWhat(info);
                        todayWorkDao.insert(todayWork);
                        Log.e("log","插入完毕");
                        makeInfo();
                    }

                    @Override
                    public void onCloseClick() {

                    }
                });
            }
        });
        makeInfo();
    }


    public void makeInfo(){
        listSecond = todayWorkDao.loadAll();
        if (listSecond.size() == 0) {
            Toast.makeText(XiaomiActivity.this,"暂无事件",Toast.LENGTH_SHORT).show();
            return;
        }

        list.clear();
        for(TodayWork todayWork : listSecond){
            if (dataTime.equals(todayWork.getWhen())) {
                list.add(todayWork);
                Log.e("log","日期-->"+ todayWork.getWhen() + "  事件-->"+ todayWork.getWhat());
            }
        }

        mList.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(XiaomiActivity.this).inflate(android.R.layout.simple_list_item_1, null);
                }
                TextView textView = (TextView) convertView;
                textView.setText(list.get(position).getWhat());
                return convertView;
            }
        });
    }

    @OnClick(R.id.back)
    public void onClick() {
        finish();
    }
}
